﻿#region System Namespace
using System;
using System.Linq;
using System.Threading;
#endregion System Namespace

#region Foreign Namespace
#endregion Foreign Namespace

#region Styx Namespace
using Styx;
using Styx.CommonBot.Frames;
using Styx.Plugins;
using Styx.WoWInternals.WoWObjects;
using Styx.WoWInternals;
using Styx.Helpers;
#endregion Styx Namespace

namespace Auction
{
    public partial class Auction : HBPlugin
    {
        public void Post()
        {
            foreach (WoWItem item in StyxWoW.Me.BagItems.Where(item => item != null))
            {
                try
                {
                    if (AuctionFrameVisible() &&
                        StyxWoW.Me.BagItems != null &&
                        new AuctionFrame().NumOwnerAuctions < MIRsettings.Instance.MaxNumberOfAuctions)
                    {
                        Random r = new Random();
                        Log("Auctioning Items...");
                        Thread.Sleep(r.Next(1050, 1150));
                        Lua.DoString("RunMacroText(\"/click IMA_AddAllButton\")");
                        Thread.Sleep(r.Next(1050, 1150));
                        Lua.DoString("RunMacroText(\"/click IMA_AuctionsSubmitButton\")");
                        Thread.Sleep(r.Next(1050, 1150));
                        Lua.DoString("RunMacroText(\"/click IMA_AcceptSendFrameSubmitButton\")");
                        Thread.Sleep(r.Next(1050, 1150));
                        KeyboardManager.KeyUpDown((char) KeyboardManager.eVirtualKeyMessages.VK_1);
                        Thread.Sleep(r.Next(1050, 1150));
                        KeyboardManager.KeyUpDown((char)KeyboardManager.eVirtualKeyMessages.VK_1);
                        Thread.Sleep(r.Next(1050, 1150));
                        KeyboardManager.KeyUpDown((char)KeyboardManager.eVirtualKeyMessages.VK_1);
                        Thread.Sleep(r.Next(1050, 1150));
                        KeyboardManager.KeyUpDown((char)KeyboardManager.eVirtualKeyMessages.VK_1);
                        Thread.Sleep(r.Next(1050, 1150));
                        KeyboardManager.KeyUpDown((char)KeyboardManager.eVirtualKeyMessages.VK_1);
                        Thread.Sleep(r.Next(1050, 1150));
                        KeyboardManager.KeyUpDown((char)KeyboardManager.eVirtualKeyMessages.VK_1);
                        Thread.Sleep(r.Next(1050, 1150));
                        KeyboardManager.KeyUpDown((char)KeyboardManager.eVirtualKeyMessages.VK_1);
                        Thread.Sleep(r.Next(1050, 1150));
                        KeyboardManager.KeyUpDown((char)KeyboardManager.eVirtualKeyMessages.VK_1);
                        Thread.Sleep(r.Next(1050, 1150));
                        KeyboardManager.KeyUpDown((char)KeyboardManager.eVirtualKeyMessages.VK_1);
                        Thread.Sleep(r.Next(1050, 1150));
                        KeyboardManager.KeyUpDown((char)KeyboardManager.eVirtualKeyMessages.VK_1);
                        Thread.Sleep(r.Next(1050, 1150));
                        KeyboardManager.KeyUpDown((char)KeyboardManager.eVirtualKeyMessages.VK_1);
                        Thread.Sleep(r.Next(1050, 1150));
                        KeyboardManager.KeyUpDown((char)KeyboardManager.eVirtualKeyMessages.VK_1);
                        Thread.Sleep(r.Next(1050, 1150));
                        KeyboardManager.KeyUpDown((char)KeyboardManager.eVirtualKeyMessages.VK_1);
                        Thread.Sleep(r.Next(1050, 1150));
                        KeyboardManager.KeyUpDown((char)KeyboardManager.eVirtualKeyMessages.VK_1);
                        Thread.Sleep(r.Next(1050, 1150));
                        KeyboardManager.KeyUpDown((char)KeyboardManager.eVirtualKeyMessages.VK_1);
                        Thread.Sleep(r.Next(1050, 1150));
                        KeyboardManager.KeyUpDown((char)KeyboardManager.eVirtualKeyMessages.VK_1);
                        Thread.Sleep(r.Next(1050, 1150));
                        KeyboardManager.KeyUpDown((char)KeyboardManager.eVirtualKeyMessages.VK_1);
                        Thread.Sleep(r.Next(1050, 1150));
                        KeyboardManager.KeyUpDown((char)KeyboardManager.eVirtualKeyMessages.VK_1);
                        Thread.Sleep(r.Next(1050, 1150));
                        Lua.DoString("RunMacroText(\"/click IMA_AuctionsClearButton\")");
                        Thread.Sleep(r.Next(1050, 1150));
                    }
                    while (new AuctionFrame().NumOwnerAuctions > MIRsettings.Instance.MaxNumberOfAuctions)
                    {
                        Log("We have reached our max number of auctions...");
                    }
                }
                catch (Exception e)
                {
                    Log(e.Message);
                    Log(e.Data.ToString());
                }
            }
        }
    }
}
